package org.atf.model.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.atf.model.entity.Preference;
import org.atf.model.entity.Server;
import org.atf.model.utils.EntityManagerUtil;

public class ServerDAO {
	private  static Logger  logger = Logger.getLogger(ServerDAO.class);
	public List<Server> findAllServer() {
		EntityManager entityManager=EntityManagerUtil.getEntityManager();
		Query query = entityManager.createQuery("select s from Server s");
		List<Server> resultList = query.getResultList();
		return resultList;
	}
	public void insertServer(Server server) { 
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		try{
			entityManager.getTransaction().begin();
			entityManager.persist(server);
			entityManager.getTransaction().commit();
		}finally {
			//entityManager.close();
		}
	}
	public void updateServer(Server server){
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		try{
			entityManager.getTransaction().begin();
			entityManager.merge(server);
			entityManager.getTransaction().commit();
		}finally {
			//entityManager.close();
		}
		
	}
	public void deleteServer(int id){
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		 try{
				entityManager.getTransaction().begin();
				Server server=entityManager.find(Server.class, id);
				entityManager.remove(server);
				entityManager.flush();
				entityManager.getTransaction().commit();
		 } finally {
			// entityManager.close();
		 }	
	}
	public Server getServer(Integer id){
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		Server server;
		 try{
				entityManager.getTransaction().begin();
				server=entityManager.find(Server.class, id);
				entityManager.getTransaction().commit();
		 } finally {
			// entityManager.close();
		 }	
		 return server;
	}
	public List<Preference> getPreference(Integer serverId){
	//	EntityManager entityManager = EntityManagerUtil.getEntityManager();
		List<Preference> prefList = new ArrayList<Preference>();
		try{
			prefList.addAll(getServer(serverId).getPreference());
		}finally {
			//entityManager.close();
		}
		return prefList;
	}
	public void insertPreference(Integer serverId, Preference preference) {
		// TODO Auto-generated method stub
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		 try{
			 entityManager.getTransaction().begin();
			 entityManager.persist(preference);
			 Server server=entityManager.find(Server.class, serverId);
			 server.addPreference(preference);
			 entityManager.merge(server);
			 entityManager.getTransaction().commit();
			 
		 }finally {
			// entityManager.close();
		 }	
	}
	public void deletePreference(Integer serverId, Integer prefId) {
		// TODO Auto-generated method stub
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		
		 try{
				entityManager.getTransaction().begin();
				Server server=entityManager.find(Server.class, serverId);
				Preference pref = null;
				for(Preference preference : server.getPreference()) {
					if(preference.getId() == prefId){
						pref= preference;
						//entityManager.remove(preference);
					}
				}
				if(pref != null){
					server.removePreference(pref);
				}
				entityManager.remove(pref);
				entityManager.merge(server);
				entityManager.getTransaction().commit();
				
				
		 } finally {
			// entityManager.close();
		 }	
	}
	public void updateServeStatusById(Integer serverid, String status){
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
	
		 try{
				entityManager.getTransaction().begin();
				Server server=entityManager.find(Server.class, serverid);
				if(server != null){
					server.setStatus(status);
					entityManager.merge(server);
				}
				entityManager.getTransaction().commit();
		 } finally {
			// entityManager.close();
		 }	
		 
	}
	public void updateServerStatus(String cellName, String nodeName,
			String serverName, String status) {
		// TODO Auto-generated method stub
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		try{
			entityManager.getTransaction().begin();
		//	String sql="UPDATE Server s SET s.status = '"+status+"' WHERE s.cellName = '"+cellName+"' and s.nodename ='"+nodeName+"' and s.servername='"+serverName+"'";
		//	logger.info(sql);
		////	Query query= entityManager.createQuery(sql);
		//	int rowCount = query.executeUpdate();
			//
			
			Query q = entityManager.createQuery("UPDATE Server s SET s.status = :status WHERE s.cellName = :cellname  and s.nodename = :nodname and s.servername= :servername");
			q.setParameter("status", status);	
			q.setParameter("cellname", cellName);	
			q.setParameter("nodename", nodeName);
			q.setParameter("servername", serverName);
			int rowCount = q.executeUpdate();		
			logger.info("update: "+rowCount );
			entityManager.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			// entityManager.close();
		 }	
		
	}
	public void mergeServer(Server oServer) {
		// TODO Auto-generated method stub
		EntityManager entityManager = EntityManagerUtil.getEntityManager();
		entityManager.getTransaction().begin();
		Server server=entityManager.find(Server.class, oServer.getId());
		server.updateProperties(oServer);
		entityManager.merge(server);
		entityManager.getTransaction().commit();
		 
	}

	
}
